// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ExpoRefPriceRule.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge
{
    #region Using Directives

    using Org.OpenTrader.Framework.Forge.Enums;
    using Org.OpenTrader.Framework.Forge.Interfaces;

    #endregion

    /// <summary>
    /// The ref price rule.
    /// </summary>
    public class ExpoRefPriceRule
    {
        #region Public Methods

        /// <summary>
        /// The standard.
        /// </summary>
        /// <param name="i">
        /// The instrument i.
        /// </param>
        /// <returns>
        /// The standard.
        /// </returns>
        public static double Standard(IInstrument i)
        {
            if (i == null)
            {
                return 0.0;
            }

            // MidPrice
            if (i.MarketBook != null)
            {
                if (i.MarketBook.MidPrice() > Constants.Zero)
                {
                    return i.MarketBook.MidPrice();
                }
            }

            // LastPrice or ClosePrice
            if (i.MarketFields != null)
            {
                // LastPrice
                if (i.MarketFields[EMarketField.Last] != null)
                {
                    if (i.MarketFields[EMarketField.Last].HasValue)
                    {
                        if (i.MarketFields[EMarketField.Last] > Constants.Zero)
                        {
                            return i.MarketFields[EMarketField.Last];
                        }
                    }
                }

                // ClosePrice
                if (i.MarketFields[EMarketField.Close] != null)
                {
                    if (i.MarketFields[EMarketField.Close].HasValue)
                    {
                        if (i.MarketFields[EMarketField.Close] > Constants.Zero)
                        {
                            return i.MarketFields[EMarketField.Close];
                        }
                    }
                }
            }

            return 0.0;
        }

        #endregion
    }
}